package concurrent;

import java.util.concurrent.CountDownLatch;

public class Result {

    static StringBuffer shared = new StringBuffer();
    static int count = 10;

    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (short i = 0; i<count;i++){
            new Thread(new Task(countDownLatch,i)).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(shared);
        System.out.println(shared.length());
    }
    static class Task implements Runnable{

        private CountDownLatch countDownLatch;
        private int data;
        public Task(CountDownLatch countDownLatch,int data){
            this.countDownLatch = countDownLatch;
            this.data = data;
        }

        @Override
        public void run() {

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            shared.append(data);
            countDownLatch.countDown();
        }
    }
}
